﻿using AutoMapper;
using B.S.DataBase.MyProject.Domain.OutboundShipments;
using B.S.DataBase.MyProject.Domain.Sales;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using MediatR;
using T.E.ERP.Write.API.Applicantions.Command.SaleOutCommand;

namespace T.E.ERP.Write.API.Applicantions.CommandHandler.SaleOutCommandHandler
{
    public class AddSaleOutInfoCommandHandler:IRequestHandler<AddSaleOutInfoCommand, APIResult<object>>
    {
        private readonly IMapper mapper;
        private readonly IBaseRepository<SaleOutRepository> saleOutBiz;
        private readonly IBaseRepository<OrderMaterials> materialBiz;

        public AddSaleOutInfoCommandHandler(IMapper mapper, IBaseRepository<SaleOutRepository> saleOutBiz, IBaseRepository<OrderMaterials> materialBiz)
        {
            this.mapper = mapper;
            this.saleOutBiz = saleOutBiz;
            this.materialBiz = materialBiz;
        }

        public Task<APIResult<object>> Handle(AddSaleOutInfoCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            SaleOutRepository saleOutInfo = new SaleOutRepository();
            saleOutInfo.Id = Guid.NewGuid();
            saleOutInfo.BusinessType = request.BusinessType;
            saleOutInfo.BusinessType = request.BusinessType;
            saleOutInfo.DocumentType = request.DocumentType;
            saleOutInfo.DocumentStatus = request.DocumentStatus;
            saleOutInfo.CustomerCode = request.CustomerCode;
            saleOutInfo.Repository = request.Repository;
            saleOutInfo.DocumentDate = request.DocumentDate;
            saleOutInfo.SendGoodsNumber = request.SendGoodsNumber;
            saleOutInfo.SendGoodsType = request.SendGoodsType;
            saleOutInfo.CustomerName = request.CustomerName;
            saleOutInfo.BusinessDemptName = request.BusinessDemptName;
            saleOutInfo.BusinessDempt = request.BusinessDempt;
            saleOutInfo.BusinessUser = request.BusinessUser;
            saleOutInfo.Remark = request.Remark;
            saleOutInfo.QiJianDate = request.QiJianDate;
            saleOutInfo.ExamUserName = request.ExamUserName;
            saleOutInfo.DocumentStatus = request.DocumentStatus;
            int add1 = saleOutBiz.Add(saleOutInfo).Result;
            if (add1 > 0)
            {
                request.OrderMaterials.ForEach(c =>
                {
                    c.SaleCode = saleOutInfo.Id;
                });
                int add2 = materialBiz.AddRanges(request.OrderMaterials).Result;
                if (add2 > 0)
                {
                    result.Code = APIEnum.添加成功;
                    result.Msg = APIEnum.添加成功.ToString();
                }
                else
                {
                    result.Code = APIEnum.添加失败;
                    result.Msg = APIEnum.添加失败.ToString();
                }
            }
            else
            {
                result.Code = APIEnum.添加失败;
                result.Msg = APIEnum.添加失败.ToString();
            }
            return Task.FromResult(result);
        }
    }
}
